package com.xtgg.more;

import java.util.List;

import com.xtgg.R;
import com.xtgg.login.FirstActivity;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.ImageLoaderHelper;
import com.xtgg.util.PreferencesUtils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MoreActivity extends BaseActivity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more);

		((TextView) findViewById(R.id.center)).setText("更多");
		((Button) findViewById(R.id.left)).setVisibility(View.INVISIBLE);

		((TextView) findViewById(R.id.name)).setText(PreferencesUtils
				.getString(this, "nickname"));
		((TextView) findViewById(R.id.tel)).setText(PreferencesUtils.getString(
				this, "telephone"));

		ImageLoaderHelper.displayImage(R.drawable.img_default,
				(ImageView) findViewById(R.id.img),
				PreferencesUtils.getString(this, "headurl"), 5);

		findViewById(R.id.action0).setOnClickListener(this);
		findViewById(R.id.action1).setOnClickListener(this);
		findViewById(R.id.action2).setOnClickListener(this);
		findViewById(R.id.action3).setOnClickListener(this);
		findViewById(R.id.action4).setOnClickListener(this);
		findViewById(R.id.login).setOnClickListener(this);
	}

	protected void onResume()
	{
		super.onResume();
		ImageLoaderHelper.displayImage(R.drawable.img_default,
				(ImageView) findViewById(R.id.img),
				PreferencesUtils.getString(this, "headurl"), 5);
	}
	
	@Override
	public void onClick(View arg0) {
		int viewId = arg0.getId();
		switch (viewId) {
		case R.id.action0:
			startActivity(new Intent(this, UserInfoActivity.class));
			break;

		case R.id.action1:
			startActivity(new Intent(this, MessageCenterActivity.class));
			break;

		case R.id.action2:
			startActivity(new Intent(this, SearchLogActivity.class));
			break;

		case R.id.action3:
			startActivity(new Intent(this, WarningActivity.class));
			break;

		case R.id.action4:
			startActivity(new Intent(this, AboutActivity.class));
			break;
			
		case R.id.login:
			showQuery();
			break;
		}
	}
	
	void showQuery() {
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_query);
		mDialog.findViewById(R.id.left).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
					}
				});
		mDialog.findViewById(R.id.right).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
						
						List<Activity> list = ((DemoApp)getApplicationContext()).getActivityList();
						int size = list.size();
						for(int i = 0; i < size; i++)
						{
							list.get(i).finish();
						}
						
						startActivity(new Intent(MoreActivity.this,FirstActivity.class));
						PreferencesUtils.putString(MoreActivity.this, "userid","");
						finish();
					}
				});
		((TextView)mDialog.findViewById(R.id.content)).setText("确认退出该帐号？");
		mDialog.show();
	}
	
	private long mExitTime;
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) 
			{
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();	
			} else {
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
